package com.eorchis.module.auditing.ui.controller;

import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import com.eorchis.core.service.IBaseService;
import com.eorchis.core.springext.mvc.SessionConstant;
import com.eorchis.core.ui.commond.ResultState;
import com.eorchis.core.ui.controller.AbstractBaseController;
import com.eorchis.module.auditing.domain.AuditingBean;
import com.eorchis.module.auditing.service.IAuditingService;
import com.eorchis.module.auditing.ui.commond.AuditingNewValidCommond;
import com.eorchis.module.auditing.ui.commond.AuditingQueryCommond;
import com.eorchis.module.auditing.ui.commond.AuditingValidCommond;
import com.eorchis.module.classuser.service.IClassUserService;
import com.eorchis.module.classuser.ui.commond.ClassUserQueryCommond;
import com.eorchis.module.classuser.ui.commond.ClassUserValidCommond;
import com.eorchis.module.remind.service.IRemindService;
import com.eorchis.module.user.domain.User;
import com.eorchis.module.util.IPUtil;
import com.eorchis.utils.utils.PropertyUtil;

/**
 * Title: AuditingController<br>
 * Description: 班级审核人员信息管理控制器<br>
 * Company: GoldGov<br>
 * Copyright @ 2012 GoldGov .All rights reserved.<br>
 * @author Max
 * @createDate 2013-12-31
 * @version $Revision$
 */
@Controller("auditingController")
@RequestMapping(value = AuditingController.MODULE_PATH)
public class AuditingController 
		extends AbstractBaseController<AuditingValidCommond,AuditingQueryCommond> 
{
	// 模块页面根路径
	public static final String MODULE_PATH = "/module/auditing";
	// 班级审核人员信息接口
	@Autowired
	@Qualifier("com.eorchis.module.auditing.service.impl.AuditingServiceImpl")
	private IAuditingService auditingService;
	// 培训班用户关系接口
	@Resource(name = "com.eorchis.module.classuser.service.impl.ClassUserServiceImpl")
	private IClassUserService classUserService;
	
	public IBaseService getService() {
		return auditingService;
	}

	// 提醒表接口
	@Resource(name = "com.eorchis.module.remind.service.impl.RemindServiceImpl")
	private IRemindService remindService;
	
	public String getModulePath() {
		return MODULE_PATH;
	}
	/**
	 * 预审核报名人员(委托部委办班在线报名系统)
	 * @param commond
	 * @return
	 * @throws Exception
	 * @author NingSS
	 * @createDate 2012-12-30
	 */
	@RequestMapping(value = "/preAuditingUser")
	public String preAuditingUser(@ModelAttribute("result") AuditingValidCommond commond,
			HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState)throws Exception{
		com.eorchis.module.user.domain.User user=(com.eorchis.module.user.domain.User)request.getSession().getAttribute(SessionConstant.USER_INFO);
		commond.setAuditingUser(user);
		commond.setAuditingDate(new Date());
		resultState.setState(ResultState.RESULT_STATE_SUCCESS);
		return "";
	}
	/**
	 * 审核报名人员,审核单位上报学时
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author NingSS
	 * @createDate 2012-12-30
	 */
	@RequestMapping(value = "/batchAuditingUser")
	public String batchAuditingUser(@ModelAttribute("result") AuditingQueryCommond queryCommond,
			HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState)throws Exception{
		resultState.setState(ResultState.RESULT_STATE_FAIL);//默认状态为失败
		if(PropertyUtil.objectNotEmpty(queryCommond.getSearchClassUserIds())&&PropertyUtil.objectNotEmpty(queryCommond.getAuditingState())){
			ClassUserQueryCommond classUserQueryCommond=new ClassUserQueryCommond();
			classUserQueryCommond.setAuditingMsg(queryCommond.getAuditingType());
			classUserQueryCommond.setAuditingState(queryCommond.getAuditingState());
			classUserQueryCommond.setSearchClassUserIds(queryCommond.getSearchClassUserIds());
			classUserQueryCommond.setAuditingStateDateStr(queryCommond.getAuditingStateStr());
			//审核通过后更改班级人员同步状态需要班级id
			ClassUserValidCommond commond=classUserService.find(queryCommond.getSearchClassUserIds()[0]);
			classUserQueryCommond.setSearchClassID(commond.getTrainingClass().getClassId());
			
			classUserService.batchAuditing(classUserQueryCommond);//更新审核状态,和审核时间
			auditingService.batchAuditing(queryCommond);//添加审核信息
			resultState.setState(ResultState.RESULT_STATE_SUCCESS);
			resultState.setMessage("审核报名人员成功");
		}else{
			resultState.setMessage("未选中任何人员");
		}
		return "";
	}
	/**
	 * 查询审核信息(委托部委办班在线报名系统)
	 * @param queryCommondst
	 * @return
	 * @throws Exception
	 * @author HuangTF
	 * @createDate 2013-12-31
	 */
	@RequestMapping(value = "/findAuditingInfo")
	public String findAuditingInfo(@ModelAttribute("result") AuditingNewValidCommond commond,
									HttpServletRequest request, HttpServletResponse response,
									@ModelAttribute("resultState") ResultState resultState) throws Exception
	{
		AuditingQueryCommond queryCommond=new AuditingQueryCommond();
		queryCommond.setSearchClassUserId(commond.getSearchClassUserId());
		//queryCommond.setAuditingType(commond.getAuditingType());
		List<AuditingBean> auditingBeanList=auditingService.findAuditingInfo(queryCommond);
		if(auditingBeanList.size()>0){
			AuditingBean auditingBean=auditingBeanList.get(0);
			commond.setAuditingUserName(auditingBean.getUserName());
			commond.setAuditingTime(auditingBean.getAuditingStateDateStr());
			commond.setAuditingState(auditingBean.getAuditingState());
			commond.setAuditingOpinion(auditingBean.getAuditingOpinion());
			resultState.setState(ResultState.RESULT_STATE_SUCCESS);
			resultState.setMessage("查询成功");
		}
		
		
		return "";
	}
	
	/**
	 * 审核报名人员(委托部委办班在线报名系统)
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author NingSS
	 * @createDate 2016-1-7
	 */
	@RequestMapping(value = "/batchAuditingClassUser")
	public String batchAuditingClassUser(@ModelAttribute("result") AuditingQueryCommond queryCommond,
			HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState)throws Exception{
		resultState.setState(ResultState.RESULT_STATE_FAIL);//默认状态为失败
		if(PropertyUtil.objectNotEmpty(queryCommond.getSearchClassUserIds())&&PropertyUtil.objectNotEmpty(queryCommond.getAuditingState())){
			ClassUserQueryCommond classUserQueryCommond=new ClassUserQueryCommond();
			User cu=(User) request.getSession().getAttribute(SessionConstant.USER_INFO);
			classUserQueryCommond.setAuditingUserIP(IPUtil.getIP(request));
			classUserQueryCommond.setAuditingUserId(cu.getUserId());
			classUserQueryCommond.setAuditingState(queryCommond.getAuditingState());
			if(queryCommond.getAuditingState().equals(queryCommond.getAuditingState())){
				classUserQueryCommond.setAuditingDate(new Date());
			}
			classUserQueryCommond.setSearchClassUserIds(queryCommond.getSearchClassUserIds());
			classUserQueryCommond.setRejectReason(queryCommond.getRejectReason());
			//classUserQueryCommond.setAuditingStateDateStr(queryCommond.getAuditingStateStr());
			classUserService.batchAuditingClassUser(classUserQueryCommond);
			resultState.setState(ResultState.RESULT_STATE_SUCCESS);
			resultState.setMessage("审核报名人员成功");
			
			/**
			 * 添加提醒
			 * @Date 2016-05-12
			 * @author RongSL
			 * @Params  提醒类型，培训班名称，承办部委名称，提醒分类
			 * */
//			try{
//				ClassUserQueryCommond cq=new ClassUserQueryCommond();
//				cq.setSearchClassUserIds(queryCommond.getSearchClassUserIds());
//				Department dep=(Department)request.getSession().getAttribute("currentScope");
//				Object[] o=null;
//				String opera="";
//				if(queryCommond.getAuditingState()==1)
//					opera="同意,报名";
//				else if(queryCommond.getAuditingState()==2)
//					opera="驳回,报名";
//				else if(queryCommond.getAuditingState()==3)
//					opera="同意,撤销报名申请";
//				
//				for(Object obj:classUserService.findClassInfo(cq)){
//					o=(Object[])obj;
//					remindService.remindOperate(Remind.BMSH,(String)o[0],dep.getDeptCode(),RoleConstants.ROLE_BW_NAME,(String)o[1],(String)o[2],(String)o[3],opera.split(",")[0],opera.split(",")[1],(String)o[4],Remind.PXB);
//				}
//			}catch(Exception e){
//				System.out.println("报名审核提醒接口出错！");
//			}
		}else{
			resultState.setMessage("未选中任何人员");
		}
		return "";
	}
}
